<template>
  <div class="cmt-container">
    <h3>发表评论</h3>
    <hr />
    <textarea placeholder="请输入评论内容(最多输入120字)" maxlength="120" v-model="comment"></textarea>
    <mt-button type="primary" size="large" @click="postComment">发表评论</mt-button>
    <div class="cmt-list">
      <div class="cmt-item" v-for="(item, i) in commentlist" :key="item.id">
        <div
          class="cmt-title"
        >第{{ i + 1 }}楼&nbsp;&nbsp;用户:{{ item.userName }}&nbsp;&nbsp;发表时间:{{ item.addTime | dateFormat }}</div>
        <div class="cmt-body">{{ item.content === null ? '此用户很懒，什么都没留下': item.content }}</div>
      </div>
    </div>
    <mt-button type="danger" size="large" plain @click="addmore">加载更多</mt-button>
  </div>
</template>
<script>
import { Toast } from "mint-ui";
export default {
  data() {
    return {
      pageIndex: 1, //展示页数
      rows: 5, //一页的行数
      commentlist: [],
      comment: ""
    };
  },
  created() {
    this.getComment();
  },
  methods: {
    getComment() {
      this.$http
        .get(
          "comment/getComment?" + "page" + this.pageIndex + "&rows=" + this.rows
        )
        .then(result => {
          if (result.body.state === "0") {
            this.commentlist = result.body.data.commentList;
          } else {
            Toast("获取评论失败");
          }
        });
    },
    addmore() {
      this.rows = this.rows + 5;
      this.getComment();
    },
    postComment() {
      if(this.comment.trim().length === 0) {
        return Toast("评论内容不能为空！")
      }
      this.$http.post("comment/insertComment?content=" + this.comment)
      .then(function(result) {
        if(result.body.state === "0"){
          this.getComment();
        }
      })
    }
  }
};
</script>
<style lang="scss">
.cmt-container {
  h3 {
    font-size: 18px;
  }
  textarea {
    font-size: 14px;
    height: 85px;
    margin: 0;
  }
  .cmt-list {
    margin: 5px 0;
    .cmt-item {
      font-size: 13px;
      .cmt-title {
        line-height: 30px;
        background-color: #ccc;
      }
      .cmt-body {
        line-height: 35px;
        text-indent: 2em;
      }
    }
  }
}
</style>